package cn.valo.system.service.impl;

import cn.valo.system.domain.Menu;
import cn.valo.system.mapper.MenuMapper;
import cn.valo.system.service.IMenuService;
import cn.valo.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author valo
 * @since 2022-06-22
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> queryTree() {
        List<Menu> menus = menuMapper.queryAll();
        List<Menu> menus1 = new ArrayList<Menu>();
        Map<Long,Menu> menuMap =new HashMap<Long,Menu>();
        menus.stream().forEach(menu->{
            menuMap.put(menu.getId(),menu);
        });
        menus.stream().forEach(menu->{
            if(menu.getParentId()!=null){
                Menu menu1 = menuMap.get(menu.getParentId());
                List<Menu> children = menu1.getChildren();
                children.add(menu);
            }
            if(menu.getParentId()==null){
                menus1.add(menu);
            }
        });
        return menus1;
    }
}
